<?php

namespace App\Logic\Auth\Form;

use App\Core\BaseListForm;
use App\Model\UserModel;
use App\Util\UserIdentityHelper;

class UserListForm extends BaseListForm
{
    
    public $name;//姓名
    public $username;//登录账号
    public $phone;//手机号
    public $type;//账号类型
    public $start_created_at;//创建时间-开始
    public $end_created_at;//创建时间-结束
    public $merchants_id;//商户id
    public $warehouse_id;//仓库id
    public $role_id;//角色id
    public $state;//审核状态
    
    public function lists()
    {
        $where = [];
        
        if (strlen($this->name) > 0) {
            $where['name'] = ["%{$this->name}%", 'like'];
        }
        
        if (strlen($this->username) > 0) {
            $where['username'] = ["%{$this->username}%", 'like'];
        }
        
        if (strlen($this->type) > 0) {
            $where['type'] = $this->type;
        }
        
        if (intval($this->phone) > 0) {
            $where['phone'] = $this->phone;
        }
        
        if ($this->checkField($this->start_created_at) && $this->checkField($this->end_created_at)) {
            $where['created_at'] = [[$this->start_created_at, $this->end_created_at], 'between'];
        }
        
        $type = UserIdentityHelper::getType();
        if ( $type == UserModel::TYPE_INSIDE ){ //超管
            if (intval($this->merchants_id) > 0) {
                $where['merchants_id'] = $this->merchants_id;
            }
            if (intval($this->warehouse_id) > 0) {
                $where['warehouse_id'] = $this->warehouse_id;
            }
        }

        if (strlen($this->role_id) > 0) {
            $where['role_id'] = $this->role_id;
        }

        if (strlen($this->state) > 0) {
            $where['state'] = $this->state;
        }
        
        //排除自己
//        $user_id = UserIdentityHelper::getId();
//        $where['id'] = [$user_id, '!='];
        
        $model = UserModel::makeQueryModel($where, [], $this->getPagination());
        
        $model->order('id', 'DESC');

        $model->field([
            'id',
            'role_id',
            'username',
            'email',
            'name',
            'phone',
            'state',
            'type',
            'last_login_time',
            'created_at',
            'created_by',
        ]);
        
        $modelResult = $model->all();
        
        $list = [];
        foreach ($modelResult as $v) {
            $item = $v->toArray();
            $item['last_login_time'] = strtotime($v->last_login_time) > 0 ? $v->last_login_time : '';
            $list[] = $item;
        }
        
        $total = $model->lastQueryResult()->getTotalCount();
        
        $pagination = $this->getPagination();
        $pagination['total_count'] = $total;
        
        $result = [
            'list'       => $list,
            'pagination' => $pagination
        ];
        
        return $this->success($result);
    }
    
}